
<template>
  <div class="com-container">
    <div class="com-chart" ref="map"></div>
  </div>
</template>

<script>
import "../assets/js/essos.js";
export default {
  data() {
    return {
      allData: [],
      myCharts: null,
    };
  },
  methods: {
    //初始化报表
    async initCharts() {
      this.myCharts = this.$echarts.init(this.$refs.map, "essos");
      //获取地图数据
      const { data: mapData } = await this.$axios.get(
        "http://192.168.4.29/map/china.php"
      );
      //注册地图数据
      this.$echarts.registerMap("china", mapData);
      //初始化报表配置项，与数据无关
      const initOptions = {
        geo: {
          type: "map",
          map: "china",
        },
      };
      this.myCharts.setOption(initOptions);
    },
    //创建报表
    async createReports() {
      const { data } = await this.$axios.get("http://localhost:3000/map");
      //获取图例数据
      const legendData = data.map((item, index) => {
        return item.name;
      });
      //获取散点数据
      const seriesData = data.map((item, index) => {
        return {
          type: "effectScatter",
          coordinateSystem: "geo",
          name: item.name,
          data: item.children,
        };
      });

      //报表数据样式等设计
      var dataOptions = {
        title: {
          text: "▎ 商家分布",
          left: 20,
          top: 20,
          textStyle: {
            color: "#893448",
            // fontSize: 25,
          },
        },
        legend: {
          data: legendData,
          left: 20,
          bottom: 20,
          orient: "vertical",
        },
        tooltip: { show: true },
        series: seriesData,
      };
      this.myCharts.setOption(dataOptions);
    },
    // 当浏览器的大小发生变化的时候, 会调用的方法, 来完成屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.map.offsetWidth / 100) * 3.6;
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
      };
      this.myCharts.setOption(adapterOption);
      this.myCharts.resize();
    },
  },
  mounted: function () {
    this.initCharts();
    this.createReports();
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
};
</script>

<style>
.com-container{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.com-chart{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>